package com.orhon.smartcampus.modules.student.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.dao.EclassHeadteacherDao;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.entity.EclassHeadteacher;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclass;
import com.orhon.smartcampus.modules.student.gqlModel.GQLHeadTeacher;
import com.orhon.smartcampus.modules.student.mm.EclassHeadteacherMapper;
import com.orhon.smartcampus.modules.student.mm.EclassMapper;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class EclassHeadteacherFetcher implements DataFetcher {
    
	@Autowired
	EclassHeadteacherDao eclassHeadteacherDao;

    @Autowired
    TokenService tokenService;


    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);

        int id = Integer.parseInt(env.getArgument("id").toString());

        String hql = "from EclassHeadteacher where deleted_at = null and id = "+id+" and school_id = " + schoolId;
        EclassHeadteacher item = eclassHeadteacherDao.findId(hql, null);
        if(item!=null) {
        	GQLEclass gqlEclass = null;
            if (env.getSelectionSet().contains("eclass")){
                Eclass eclass = item.getStudentEclassByEclassId();
                gqlEclass = EclassMapper.MAPPER.toEclass(eclass);
            }
            
            GQLTeacher gqlTeacher = null;
            if (env.getSelectionSet().contains("teacher")){
            	TInformation tInformation = item.getTeacherTInformationByTeacherId();
            	gqlTeacher = TeacherMapper.MAPPER.toTeacher(tInformation);
            }
            
            GQLSchool gqlSchool = null;
            if (env.getSelectionSet().contains("school")){
                Schools schools = item.getBaseSchoolsBySchoolId();
                gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
            }

            GQLHeadTeacher teacher = EclassHeadteacherMapper.MAPPER.mapEntity(item);
            teacher.setEclass(gqlEclass);
            teacher.setSchool(gqlSchool);
            teacher.setTeacher(gqlTeacher);
            return teacher;
        }
		return null;
        
    }


}
